package com.chuantou.yuba.ui.fragment;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.chuantou.yuba.R;
import com.chuantou.yuba.adapter.NewsListAdapter;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.FavNewsDataTO;
import com.chuantou.yuba.api.to.FavNewsListTO;
import com.chuantou.yuba.api.to.NewsTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.ui.NewsDetailActivity;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.Constants;

/**
 * 首页资讯列表fragment
 * 
 * @author dl1001
 * 
 */
public class FavNewsListFragment extends PaginationListFragment<FavNewsDataTO> {
	private NewsListAdapter mListAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View contentView = super.onCreateView(inflater, container, savedInstanceState);
		if (mListAdapter == null) {
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					NewsTO newsTO = (NewsTO) parent.getItemAtPosition(position);
					Intent intent = new Intent(getActivity(), NewsDetailActivity.class);
					intent.putExtra(Constants.EXTRA_DATA, newsTO);
					startActivity(intent);
				}
			});
			mListAdapter = new NewsListAdapter(mContext);
			mListView.setAdapter(mListAdapter);
			startRequest();
		}
		return contentView;
	}

	@Override
	public void onResponse(FavNewsDataTO response) {
		if (getActivity() != null) {
			super.onResponse(response);
			if (response != null) {
				FavNewsListTO listTO = response.getNews();
				List<NewsTO> newsList = null;
				if (listTO != null) {
					newsList = listTO.getNewsList();
				}
				// 第一页
				if (response.getCurrentPage() <= 1) {
					mListAdapter.clear();
				}
				mListAdapter.addNewsAd(newsList);
				mListAdapter.notifyDataSetChanged();
			}
		}
	}


	@Override
	protected BaseGsonRequest<FavNewsDataTO> getRequest(Listener<FavNewsDataTO> listener, ErrorListener errorListener) {
		String url = UriHelper.getFavListUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(Constants.DEFAULT_PAGE_SIZE));
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		AccountManager accountManager = AccountManager.getInstance(mContext);
		UserTO user = accountManager.getUserTO();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		params.put(UriHelper.PARAMS_TYPE, "news");
		return new GsonRequest<FavNewsDataTO>(Method.POST, url, listener, errorListener, params, FavNewsDataTO.class);
	}

	@Override
	protected String getEmptyString() {
		return mContext.getString(R.string.no_content);
	}

}
